package com.vitas.coin.ui.act

import android.view.View
import androidx.media3.common.Player
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.BarUtils
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.snap.base.util.BaseUtil
import com.snap.common.main.BaseMVVMAct
import com.snap.util.edge.marginTopSystemWindowInsets
import com.snap.util.edge.paddingBottomSystemWindowInsets
import com.snap.util.edge.paddingTopSystemWindowInsets
import com.vitas.coin.R
import com.vitas.coin.callback.MusicAdapterCallback
import com.vitas.coin.data.bean.ChildrenVoiceBean
import com.vitas.coin.data.event.CollectEvent
import com.vitas.coin.data.event.PausePlayEvent
import com.vitas.coin.data.event.RequestFinishEvent
import com.vitas.coin.data.event.StartPlayEvent
import com.vitas.coin.databinding.ActMusicBinding
import com.vitas.coin.manager.PlayManager
import com.vitas.coin.ui.dialog.CollectListDialog
import com.vitas.coin.util.AdUtil
import com.vitas.coin.vm.HomeVM


class MusicAct : BaseMVVMAct<ActMusicBinding, HomeVM>(){

    companion object {
        const val INTENT_NAME = "NAME"
        const val PARENT_ID = "PARENT_ID"
    }

    override fun createVM(): HomeVM {
        return HomeVM().apply {
            actionAct = { this@MusicAct }
        }
    }

    var pageNumber :Int = 1

    var parentId :Long = 1



    override fun doDataBind() {
        binding.viewModel = viewModel
        binding.page = this
    }

    override fun getContentViewId(): Int {
        return R.layout.act_music
    }

    override fun onCreate() {
        binding.clContent.paddingBottomSystemWindowInsets()
        binding.rlTop.marginTopSystemWindowInsets()
//        BarUtils.addMarginTopEqualStatusBarHeight(binding.rlTop)
        BarUtils.transparentStatusBar(this)
        BarUtils.setStatusBarLightMode(this, true)
        initRv()
        initRefreshLayout()
        initBottomPlay()
        initType()
        initObserve()
        AdUtil.adInsert(this)
    }

    private fun initType() {
        val cateName = intent.getStringExtra(INTENT_NAME).toString()
        viewModel.setCateName(cateName)
        binding.tvTitle.text = cateName
        parentId = intent.getLongExtra(PARENT_ID,0L)
        viewModel.loadData(parentId,pageNumber)
    }
    private fun initRefreshLayout() {
        binding.mRefreshLayout.setRefreshHeader(ClassicsHeader(this))
        binding.mRefreshLayout.setRefreshFooter(ClassicsFooter(this))
        binding.mRefreshLayout.setEnableRefresh(true)
        binding.mRefreshLayout.setEnableLoadMore(true)
        binding.mRefreshLayout.setOnRefreshListener{
            pageNumber = 1
            viewModel.loadData(parentId,pageNumber)
        }
        binding.mRefreshLayout.setOnLoadMoreListener {
            pageNumber++
            viewModel.loadData(parentId, pageNumber)
        }
    }


    private fun initObserve() {
        BaseUtil.observer(this, StartPlayEvent::class.java) {
            if (binding.clBottom.visibility==View.GONE) {
                binding.clBottom.visibility = View.VISIBLE
            }
            viewModel.musicName.value = it.musicObj.musicName
            binding.ivPlay.setImageResource(R.mipmap.icon_pause)
            it.musicObj.id?.let { it1 -> viewModel.freshListByPlay(it1, true) }
        }

        BaseUtil.observer(this, PausePlayEvent::class.java) {
            binding.ivPlay.setImageResource(R.mipmap.icon_start)
            it.musicObj.id?.let { it1 -> viewModel.freshListByPlay(it1, false) }
        }


        BaseUtil.observer(this, CollectEvent::class.java) {
            viewModel.freshListByCollect(it.musicObj)
        }

        BaseUtil.observer(this, RequestFinishEvent::class.java) {
            binding.mRefreshLayout.finishRefresh(500)
            binding.mRefreshLayout.finishLoadMore()
        }
    }

    private fun initBottomPlay() {
        val playStatus = PlayManager.getInstance().playStatus
        if (playStatus==Player.STATE_IDLE) {
            binding.clBottom.visibility = View.GONE
        }else{
            binding.clBottom.visibility = View.VISIBLE
            viewModel.musicName.value = PlayManager.getInstance().musicName
            if (PlayManager.getInstance().isPlaying) {
                binding.ivPlay.setImageResource(R.mipmap.icon_pause)
            }else{
                binding.ivPlay.setImageResource(R.mipmap.icon_start)
            }
        }
    }

    private fun initRv() {
        binding.mRecyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        binding.mRecyclerView.adapter = viewModel.musicAdapter
        viewModel.musicAdapter.setDownloadCallBack(object :
            MusicAdapterCallback {
            override fun clickDownload(mChildrenVoiceBean: ChildrenVoiceBean?, type: Int) {
                if (mChildrenVoiceBean != null) {
                    viewModel.download(mChildrenVoiceBean,type)
                }
            }

            override fun clickCollect(mChildrenVoiceBean: ChildrenVoiceBean?) {
                if (mChildrenVoiceBean != null) {
                    viewModel.collect(mChildrenVoiceBean)
                }
            }
        })
        viewModel.musicAdapter.setType(1)
    }

    fun onClickBack(view: View) {
        finish()
    }

    fun onClickCollectList(view: View) {
        CollectListDialog(this) {

        }.showXY(binding.clBottom)
    }



}